﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UserSystemApi.Entity;
using UserSystemApi.Services.Account.Dto;

namespace UserSystemApi.Services.Account
{
    public partial class AccountService : IAccountService
    {
        public async Task<List<Users>> ListUserAll()
        {
            return await _userRepository.GetListAsync();
        }
        public async Task<Users> GetUserById(int id)
        {
            return await _userRepository.GetEntityByIdAsync(id);
        }
      
        public async Task<int> CreateAndUpdateUsers(UserInputDto userInputDto)
        {
            if (userInputDto.Id == 0)
            {
                return  await InsertUsers(userInputDto);
            }
            else
            {
                return  await UpdateUsers(userInputDto);
            }

        }
        private  async Task<int> UpdateUsers(UserInputDto userInputDto)
        {
            Users user = await  _userRepository.GetEntityByIdAsync(userInputDto.Id);
            UserInputDto.ToUpdateUsers(ref user, userInputDto);
            return await _userRepository.UpdateResultIdAsync(user);
        }
        private async Task<int> InsertUsers(UserInputDto userInputDto)
        {
            var user = UserInputDto.ToCreateUsers(userInputDto);
            return  await _userRepository.InsertResultIdAsync(user);
        }

        public async Task<bool> DeleteUserById(int id)
        {
            bool deleted = await _userRepository.DeleteAsync(id);
            return deleted;
        }
    }
}
